package com.example.android.ui.hb.detail;

import com.example.android.bean.Login;
import com.example.android.bean.SearchHB;

import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class HBDetailPresenter implements HBDetailContract.IHBDetailPresenter {
    private HBDetailContract.IHBDetailView ihbDetailView;
    HBDetailContract.IHBDetailModel ihbDetailModel;
    public HBDetailPresenter(HBDetailContract.IHBDetailView ihbDetailView){
        this.ihbDetailView = ihbDetailView;
        ihbDetailModel = new HBDetailModel();
    }


    @Override
    public void getHBDetail(int hbId) {
        ihbDetailModel.getHBDetail(hbId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<SearchHB>>() {
                    @Override
                    public void accept(List<SearchHB> searchHBList) throws Throwable {
                        ihbDetailView.getHBDetailSuccess(searchHBList.get(0));
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Throwable {
                        ihbDetailView.getHBDetailError(throwable);
                    }
                });
    }

    @Override
    public void getRelease(int userId, String token) {
        ihbDetailModel.getRelease(userId,token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<Login.UserInfoDTO>>() {
                    @Override
                    public void accept(List<Login.UserInfoDTO> userInfoDTOS) throws Throwable {
                        ihbDetailView.getReleaseSuccess(userInfoDTOS.get(0));
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Throwable {
                        ihbDetailView.getReleaseError(throwable);
                    }
                });
    }
}
